using System;

namespace SharpMud.MudLib.SAMPLE.Net.SessionStates
{
	public class GamePlayPrompt : SessionStateBase
	{
		private string	lastInput;
		private int		inputRepitition;

		public GamePlayPrompt()
		{
			lastInput = "";
			inputRepitition = 0;
		}

		public void InformUnrecognizedCommand()
		{
			Session.WriteLine();
			Session.WriteLine("Huh? That command was not recognized, try another.");
			Session.WriteLine();
		}

		public override void Pulse()
		{
			if(StillActive)
			{
				if(Session.Connection.Status==SharpMud.Net.ConnectionStatus.Disconnected)
				{
					this.SetInactive();
					return;
				}
				string input;
				input = Session.Prompt("[#black][white]SharpMud Beta[blue]> [green]");
				if(input!=null)
				{
					input = input.Trim();
					if(input==String.Empty)
					{
						lastInput="";
						inputRepitition=0;
						return;
					}
					else
					{
						if(input=="!")
						{
							input = lastInput;
						}
						try
						{
							bool b = true;
							//b = MudServer.CommandManager.FindCommand(Session.CurrentCharacter,input.Trim());
							if(b)
							{
								MudServer.CommandManager.ExecuteCommand(Session.CurrentCharacter,input.Trim());
							}
							else
							{
								this.InformUnrecognizedCommand();
							}
						}
						catch(Commands.CommandNotFoundException e)
						{
							this.InformUnrecognizedCommand();
						}
						if(lastInput==input)
						{
							inputRepitition++;
						}
						else
						{
							lastInput=input;
							inputRepitition=0;
						}
					}
				}
				else
				{
					if(Session.Connection.Status==SharpMud.Net.ConnectionStatus.Disconnected)
					{
						this.SetInactive();
						return;
					}					
				}
			}
		}

		public override string StateID
		{
			get
			{
				return "GamePlayPrompt";
			}
		}
	}
}